package com.test;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @description
 * 
 * @author 谢进伟
 * 
 * @createDate 2016年10月14日 下午3:32:15
 */
public class Demo {
	
	private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	/**
	 * 将信息记录到日志文件
	 * 
	 * @param logFile
	 *            日志文件
	 * @param mesInfo
	 *            信息
	 * @throws IOException
	 */
	public static void logMsg(File logFile , String mesInfo) throws IOException {
		if(logFile == null) {
			throw new IllegalStateException("logFile can not be null!");
		}
		Writer txtWriter = new FileWriter(logFile , true);
		txtWriter.write(dateFormat.format(new Date()) + "\t" + mesInfo + "\n");
		txtWriter.flush();
	}
	
	public static void main(String [] args) throws Exception {
		
		final File tmpLogFile = new File("D:/JavaTools/Tomcat8.0/logs/catalina.out");
		if(!tmpLogFile.exists()) {
			tmpLogFile.createNewFile();
		}
		// 启动一个线程每5秒钟向日志文件写一次数据
		ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);
		exec.scheduleWithFixedDelay(new Runnable() {
			
			public void run() {
				try {
					logMsg(tmpLogFile , " 99bill saveSendEmailLog !");
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
		} , 0 , 5 , TimeUnit.SECONDS);
	}
}
